#include "VBomb.h"

VBomb::VBomb(QWidget *parent, int time, int pos_x, int pos_y, int extra, int direction) : QLabel(parent)
{
    this->parent = parent;
    this->extra = extra;
    this->direction = direction;
    if (extra == 0)
        this->bomb_image = new QPixmap(MPictures::get_instance()->m_pictures.value("bomb"));
    else
        this->bomb_image = new QPixmap(MPictures::get_instance()->m_pictures.value("fire"));
    this->setPixmap(*(this->bomb_image));
    this->move(pos_x, pos_y);
    this->timer = new QTimer(this);
    QObject::connect(this->timer, SIGNAL(timeout()), this, SLOT(crash()));
    this->pos_x = pos_x;
    this->pos_y = pos_y;
    this->timer->start(time);
    this->show();
}

VBomb::~VBomb()
{
    delete this->timer;
}

void VBomb::crash()
{
    if (this->extra > 3)
    {
        delete this;
        return;
    }
    else if (this->extra == 0)
    {
        new VBomb(this->parent, 100, this->pos_x, this->pos_y - 10, this->extra + 1, 0);
        new VBomb(this->parent, 100, this->pos_x + 10, this->pos_y, this->extra + 1, 1);
        new VBomb(this->parent, 100, this->pos_x, this->pos_y + 10, this->extra + 1, 2);
        new VBomb(this->parent, 100, this->pos_x - 10, this->pos_y, this->extra + 1, 3);
    }
    else if (this->direction == 0)
        new VBomb(this->parent, 100, this->pos_x, this->pos_y - 10, this->extra + 1, 0);
    else if (this->direction == 1)
        new VBomb(this->parent, 100, this->pos_x + 10, this->pos_y, this->extra + 1, 1);
    else if (this->direction == 2)
        new VBomb(this->parent, 100, this->pos_x, this->pos_y + 10, this->extra + 1, 2);
    else if (this->direction == 3)
        new VBomb(this->parent, 100, this->pos_x - 10, this->pos_y, this->extra + 1, 3);
    delete this;
}
